package org.jeecg.modules.zhima.portal.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.jeecg.modules.zhima.base.common.Result;
import org.jeecg.modules.zhima.base.util.BigDecimalUtils;
import org.jeecg.modules.zhima.center.entity.OmsCartItem;
import org.jeecg.modules.zhima.center.service.IOmsCartItemService;
import org.jeecg.modules.zhima.portal.domain.Cart;
import org.jeecg.modules.zhima.portal.domain.CartItem;
import org.jeecg.modules.zhima.portal.domain.CartParam;
import org.jeecg.modules.zhima.portal.domain.CartTotal;
import org.jeecg.modules.zhima.portal.service.ICartItemService;
import org.jeecg.modules.zhima.portal.service.ICartService;
import org.jeecg.modules.zhima.portal.service.IOrderService;
import org.jeecg.modules.zhima.portal.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 购物车管理Controller
 * Created by dingzg on 2018/8/2.
 */
@Controller
@Api(tags = "OmsCartItemController", description = "购物车管理")
@RequestMapping("/cart")
public class MOmsCartItemController {
    @Autowired
    private IOmsCartItemService omsCartItemService;
    @Autowired
    private ICartItemService cartItemService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private ICartService cartService;

    @ApiOperation("添加商品到购物车")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result add(@RequestBody OmsCartItem cartItem) {
        return Result.ok(cartItemService.add(cartItem));
    }

    @ApiOperation("获取某个会员的购物车列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Result list() {
        Cart cart = new Cart();
        List<CartItem> cartItemList = cartService.list(userService.getCurrentMember().getId());
        CartTotal cartTotal = new CartTotal();
        if (CollectionUtils.isNotEmpty(cartItemList)){
            List<CartItem> online = new ArrayList<>();
            List<CartItem> offline = new ArrayList<>();
            for (CartItem item : cartItemList) {
                if (item.getProduct().getProduct().getPublishStatus() == 1){
                    online.add(item);
                }else {
                    offline.add(item);
                }
            }
            cart.setCartItems(online);
            cart.setOfflineItems(offline);
            cartTotal.setCount(online.size());
            BigDecimal amount = new BigDecimal(0);
            BigDecimal checkAmount = new BigDecimal(0);
            for (int i = 0; i < online.size(); i++) {
                CartItem item = online.get(i);
                amount = BigDecimalUtils.add(amount, BigDecimalUtils.multiply(item.getPrice(), item.getQuantity()));
                cartTotal.setCount(cartTotal.getCount() + item.getQuantity());
                if (item.getIsChecked() == 1){
                    cartTotal.setCheckedCount(cartTotal.getCheckedCount() + item.getQuantity());
                    checkAmount = BigDecimalUtils.add(checkAmount, BigDecimalUtils.multiply(item.getPrice(), item.getQuantity()));
                }
            }
            cartTotal.setAmount(amount);
            cartTotal.setCheckedAmount(checkAmount);
        }
        cart.setCartTotal(cartTotal);
        return Result.success(cart);
    }

    @ApiOperation("获取购物车商品件数")
    @RequestMapping(value = "/goodscount", method = RequestMethod.GET)
    @ResponseBody
    public Result listPromotion(@RequestParam(required = false) Integer productId) {
        return Result.success(omsCartItemService.list(userService.getCurrentMember().getId()));
    }

//    @ApiOperation("获取某个会员的购物车列表,包括促销信息")
//    @RequestMapping(value = "/list/promotion", method = RequestMethod.GET)
//    @ResponseBody
//    public Result listPromotion(@RequestParam(required = false) List<Long> cartIds) {
//        List<CartPromotionItem> cartPromotionItemList = cartItemService.listPromotion(memberService.getCurrentMember().getId(), cartIds);
//        return Result.success(cartPromotionItemList);
//    }
//

    @ApiOperation("修改购物车中某个商品的数量")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Result updateQuantity(@RequestBody CartParam cart) {
        omsCartItemService.update(cart);
        return this.list();
    }
//
//    @ApiOperation("获取购物车中某个商品的规格,用于重选规格")
//    @RequestMapping(value = "/getProduct/{productId}", method = RequestMethod.GET)
//    @ResponseBody
//    public Result getCartProduct(@PathVariable Long productId) {
//        CartProduct cartProduct = cartItemService.getCartProduct(productId);
//        return Result.success(cartProduct);
//    }
//
//    @ApiOperation("修改购物车中商品的规格")
//    @RequestMapping(value = "/update/attr", method = RequestMethod.POST)
//    @ResponseBody
//    public Result updateAttr(@RequestBody OmsCartItem cartItem) {
//        int count = cartItemService.updateAttr(cartItem);
//        if (count > 0) {
//            return Result.success(count);
//        }
//        return Result.failed();
//    }

    @ApiOperation("选择商品")
    @RequestMapping(value = "/checked", method = RequestMethod.POST)
    @ResponseBody
    public Result checked(@RequestBody CartParam cart) {
        omsCartItemService.checked(cart.getIds(), cart.getIsChecked());
        return this.list();
    }

    @ApiOperation("结算确认")
    @RequestMapping(value = "/checkout", method = RequestMethod.GET)
    @ResponseBody
    public Result checkout(@RequestParam(required = false) Integer id,
                           @RequestParam(required = false) Integer addressId,
                           @RequestParam(required = false) Integer couponId) {
        return Result.success(orderService.generateConfirmOrder(id, addressId, couponId));
    }

    @ApiOperation("删除购物车中的某个商品")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestBody CartParam cart) {
        omsCartItemService.delete(userService.getCurrentMember().getId(), cart.getIds());
        return list();
    }

    @ApiOperation("清空购物车")
    @RequestMapping(value = "/clear", method = RequestMethod.POST)
    @ResponseBody
    public Result clear() {
        omsCartItemService.clear(userService.getCurrentMember().getId());
        return list();
    }
}
